﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.VisiBoard.Controls;
using VNet.VisiSuite.VisiBoard.ViewModels;
using VNet.VisiSuite.VisiBoard.Views;

namespace VNet.VisiSuite.VisiBoard.Common.Helpers
{
    public static class NavigationManager
    {
        #region Fields
        private static bool _wflag = false;
        private static VisiPage _currentPage = null;
        private static ContentControl _inactiveContent = null;
        private static ContentControl _activeContent = null;
        private static DoubleAnimation _animation = null;
        private static MainWindow _main_window;

        private static ContentControl _dialog_content = null;
        private static Storyboard _show_dialog = null;
        private static Storyboard _hide_dialog = null;
        private static Storyboard _show_keyboard = null;
        private static Storyboard _hide_keyboard = null;

        private static bool _is_keyboard_open = false;

        #endregion

        public static CellItem CurrentCell { get; set; }

        public static void SetMainWindow(MainWindow window)
        {
            _main_window = window;

            if (_main_window == null) return;

            _main_window.MouseRightButtonDown += _main_window_MouseRightButtonDown;
            _main_window.MouseLeftButtonDown += _main_window_MouseLeftButtonDown;
            _main_window.KeyDown += _main_window_KeyDown;

            _activeContent = _main_window._content1;
            _inactiveContent = _main_window._content2;

            _main_window._topPanel.OnCloseCallback(HidePanel);
            _main_window._bottomPanel.OnCloseCallback(HidePanel);

            _dialog_content = _main_window._dialog_content;
            _show_dialog = (Storyboard)_main_window.FindResource("_show_dialog");
            _hide_dialog = (Storyboard)_main_window.FindResource("_hide_dialog");
            _show_keyboard = (Storyboard)_main_window.FindResource("_show_keyboard");
            _hide_keyboard = (Storyboard)_main_window.FindResource("_hide_keyboard");

            NavigateTo(new ConfigurationView(), null);
        }

        private static void _main_window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F11)
            {
                switch (_wflag)
                {
                    case false:
                        _main_window.WindowState = WindowState.Normal;
                        _main_window.WindowStyle = WindowStyle.SingleBorderWindow;
                        _main_window.Topmost = false;
                        _main_window.ResizeMode = ResizeMode.CanResize;
                        _wflag = true;
                        break;
                    case true:
                        _main_window.Visibility = Visibility.Collapsed;
                        _main_window.WindowState = WindowState.Maximized;
                        _main_window.WindowStyle = WindowStyle.None;
                        _main_window.Topmost = true;
                        _main_window.ResizeMode = ResizeMode.NoResize;
                        _main_window.Visibility = Visibility.Visible;
                        _wflag = false;
                        break;
                }
            }
            else if (_currentPage != null)
            {
                _currentPage.DoKeyDown(e);
            }
        }

        private static void _main_window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            double yy = e.GetPosition(_main_window).Y;

            if (!_main_window._topPanel.Contains(yy) && !_main_window._bottomPanel.Contains(_main_window._main_Grid.RenderSize.Height, yy))
                HidePanel();
        }

        private static void _main_window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            if (!_main_window._topPanel.IsHidden() || !_main_window._bottomPanel.IsHidden())
            {
                _main_window._topPanel.Hide();
                _main_window._bottomPanel.Hide();
            }
            else
            {
                _main_window._topPanel.Show();
                _main_window._bottomPanel.Show();
            }
        }

        public static void HidePanel()
        {
            if (_main_window == null) return;

            _main_window._topPanel.Hide();
            _main_window._bottomPanel.Hide();
        }

        public static void ShowPanel(PanelType panelType)
        {
            if (_main_window == null) return;

            if (panelType == PanelType.Top || panelType == PanelType.Both)
                _main_window._topPanel.Show();
            if (panelType == PanelType.Bottom || panelType == PanelType.Both)
                _main_window._bottomPanel.Show();
        }

        public static void NavigateTo(VisiPage page, object navigationObj)
        {
            if (_main_window == null) return;

            HidePanel();

            if (page != null && page != _currentPage)
            {
                //Invoke OnLeave method to clear the previous View
                if (_currentPage != null)
                {
                    var oldViewModel = _currentPage.DataContext as BaseViewModel;
                    if (oldViewModel != null)
                        oldViewModel.OnLeave();
                }

                // clear out old menu items
                _main_window._topPanel.SetTiles(string.Empty, null);
                _main_window._bottomPanel.SetIcons(null);
                _main_window._bottomPanel.UserControl(null);
                _activeContent.IsEnabled = false;

                _inactiveContent.SetValue(Panel.ZIndexProperty, 0);
                _inactiveContent.Content = page;

                TranslateTransform transform;
                if (_inactiveContent == _main_window._content1)
                    transform = _main_window._content1Translate;
                else
                    transform = _main_window._content2Translate;

                transform.X = _activeContent.ActualWidth;
                _inactiveContent.IsEnabled = true;
                //_inactiveContent.Visibility = Visibility.Visible;

                _inactiveContent.SetValue(Panel.ZIndexProperty, 100);

                var duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
                _animation = new DoubleAnimation(_activeContent.ActualWidth, 0, duration);
                _animation.Completed += delegate(object sender, EventArgs e)
                {
                    _animation.FillBehavior = FillBehavior.Stop;

                    //_activeContent.Visibility = Visibility.Hidden;
                    _activeContent.SetValue(Panel.ZIndexProperty, 0);
                    transform.X = _activeContent.ActualWidth;
                    _activeContent.Content = null;

                    ContentControl swap = _activeContent;
                    _activeContent = _inactiveContent;
                    _inactiveContent = swap;

                    //// add new menus
                    _main_window._topPanel.SetTiles(_currentPage.TopMenuDescription(), _currentPage.TopMenuItems());
                    _main_window._bottomPanel.SetIcons(_currentPage.BottomMenuItems());
                    _main_window._bottomPanel.UserControl(_currentPage.BottomMenuControl());

                    _activeContent.SetValue(Panel.ZIndexProperty, 50);

                    //Invoke OnNavigate method to get data on next View
                    var newViewModel = _currentPage.DataContext as BaseViewModel;
                    if (newViewModel != null)
                        newViewModel.OnNavigate(navigationObj);
                };

                transform.BeginAnimation(TranslateTransform.XProperty, _animation);
                _currentPage = page;
            }
        }

        private static void show_keyboard()
        {
            if (_show_keyboard != null)
            {
                //from double of keyboard height and -negative keyboard's with
                _main_window._keyboard_translate.Y = (_main_window.Height * 8) / 10; 
                _main_window._keyboard_translate.X = -(_main_window.Width - 375);
                _show_keyboard.Begin();
            }
        }

        private static void hide_keyboard()
        {
            if (_hide_keyboard != null)
            {
                _main_window._keyboard_translate.Y = (_main_window.Height * 8) / 10;
                _main_window._keyboard_translate.X = -(_main_window.Width - 375);
                _hide_keyboard.Begin();
            }
        }

        public static void showDialog(VisiDialog dlg)
        {
            if (_dialog_content.Content != null)
            {
                var visiDialog = _dialog_content.Content as VisiDialog;
                if (visiDialog != null)
                {
                    visiDialog.InputKeyBoardClick -= OnInputKeyBoardClick;
                    visiDialog.InputKeyBoardClick = null;
                    visiDialog.close(false);  // prevent recursive call to close callback
                }
            }
            _dialog_content.Content = dlg;
            dlg.InputKeyBoardClick += OnInputKeyBoardClick;
            if (_show_dialog != null)
            {
                _show_dialog.Begin();
            }
        }

        private static void OnInputKeyBoardClick(object sender, EventArgs args)
        {
            _is_keyboard_open = ! _is_keyboard_open;
            if (_is_keyboard_open)
                show_keyboard();
            else
                hide_keyboard();
        }   

        

        public static void hideDialog(bool showAnimation = true)
        {
            _dialog_content.Content = null;
            
            if (_is_keyboard_open)
            {
                _is_keyboard_open = false;
                hide_keyboard();
            }
            if (_hide_dialog != null && showAnimation)
                _hide_dialog.Begin();
        }

        public static void refreshDialog()
        {
            if (_dialog_content.Content != null)
            {
                ((VisiDialog)_dialog_content.Content).refresh();
            }
        }

        public static void CloseProgram()
        {
            if (_main_window != null)
                _main_window.Close();
        }
    }

    public enum PanelType
    {
        Top,
        Bottom,
        Both
    }
}